package services;

import domain.entities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nico
 * Date: 07/11/12
 * Time: 16:18
 * To change this template use File | Settings | File Templates.
 */
@Service
public class OngoingService {

    private OngoingDAO ongoingDAO;
    private ProtocoloCompromisopaisDAO protocoloCompromisopaisDAO;
    private InvestigadorProtocoloCentroService investigadorProtocoloCentroService;
    private CentroDAO centroDAO;

    public OngoingService() {
    }

    @Autowired
    public OngoingService(OngoingDAO ongoingDAO, ProtocoloCompromisopaisDAO protocoloCompromisopaisDAO, InvestigadorProtocoloCentroService investigadorProtocoloCentroService, CentroDAO centroDAO) {
        this.ongoingDAO = ongoingDAO;
        this.protocoloCompromisopaisDAO = protocoloCompromisopaisDAO;
        this.investigadorProtocoloCentroService = investigadorProtocoloCentroService;
        this.centroDAO = centroDAO;
    }

    public Ongoing findById(Integer id){
        Ongoing o = ongoingDAO.findById(id);
        return o;
    }

    public void save(ProtocoloCompromisopais pcp, Ongoing ongoing, Integer investigatorId) {

        InvestigadorProtocoloCentro investigadorProtocoloCentro = investigadorProtocoloCentroService.findByInvestigatorAndProtocoloCompromisoPais(pcp, investigatorId);
        ongoing.setInvestigadorProtocoloCentro(investigadorProtocoloCentro);
        investigadorProtocoloCentro.setOngoing(ongoing);
        Centro centro = investigadorProtocoloCentro.getProtocoloCentro().getCentro();
        if(ongoing.getInitialPackageSent() != null)
            centro.setStatus("Start Up");
        if(ongoing.getSiteActivation() != null)
            centro.setStatus("Enrolling");
        if(ongoing.getActualLpfv() != null)
            centro.setStatus("Enrollment Closed");
        if(ongoing.getSiteClosure() != null)
            centro.setStatus("Closed");
        centroDAO.merge(centro);
        ongoing.setLastModification(Calendar.getInstance().getTime());
        ongoingDAO.merge(ongoing);
        this.status(pcp, centro);
    }

    private void status(ProtocoloCompromisopais pcp, Centro centro) {
        ArrayList<Ongoing> all = ongoingDAO.findByProtocoloCompromisoPais(pcp);
        String newStatus = "";

        for(Ongoing o : all){
            if(o.getInvestigadorProtocoloCentro().getProtocoloCentro().getCentro().getStatus().equals("Start Up"))
                newStatus = "Start Up";
        }

        for(Ongoing o : all){
            if(o.getInvestigadorProtocoloCentro().getProtocoloCentro().getCentro().getStatus().equals("Enrolling"))
                newStatus = "Enrolling";
        }

        if(newStatus != null){
            if(pcp.getStatus() == null || !pcp.getStatus().equals("Enrolling")){
                pcp.setStatus(newStatus);
            }
        }

        boolean allSameStatus = true;
        for(Ongoing o : all){
            if(!o.getInvestigadorProtocoloCentro().getProtocoloCentro().getCentro().getStatus().equals("Enrollment Closed"))
                allSameStatus = false;
        }
        if(allSameStatus)
            pcp.setStatus("Enrollment Closed");

        allSameStatus = true;
        for(Ongoing o : all){
            if(!o.getInvestigadorProtocoloCentro().getProtocoloCentro().getCentro().getStatus().equals("Closed"))
                allSameStatus = false;
        }
        if(allSameStatus)
            pcp.setStatus("Closed");

        if(pcp.getStatus().equals("Closed") && centro.getStatus().equals("Enrollment Closed"))
            pcp.setStatus("Enrollment Closed");

        protocoloCompromisopaisDAO.merge(pcp);
    }

    public List<Ongoing> findAll() {
        return ongoingDAO.findAll();
    }
}
